<?php
class ccdsSession {
   // session-lifetime
   var $lifeTime;
   // mysql-handle
   var $dbHandle;
   private $ccdSQL;
   
   function __construct() {
   	$this->ccdSQL = ccdSQL::getInstance();
  }
   		
   
   function open($savePath, $sessName) {
       // get session-lifetime
       $this->lifeTime = get_cfg_var("session.gc_maxlifetime");
       return true;
   }
   function close() {
       $this->gc(ini_get('session.gc_maxlifetime'));
       // close database-connection
//       return @$this->ccdSQL->close();
   }
   function read($sessID) {
       // fetch session-data
       $save = $this->ccdSQL->bMasterOnly;
       $this->ccdSQL->bMasterOnly = true;
       $res = $this->ccdSQL->query("SELECT session_data AS d FROM ws_sessions
                           WHERE session_id = '$sessID'
                            AND session_host = '".$_SERVER['HTTP_HOST']."'
                           AND session_expires > ".time(),true);

/*			file_put_contents('./post.log',"SELECT session_data AS d FROM ws_sessions
                           WHERE session_id = '$sessID'
                            AND session_host = '".$_SERVER['HTTP_HOST']."'
                           AND session_expires > ".time()."\n\n",FILE_APPEND);
*/       
       // return data or an empty string at failure
       $this->ccdSQL->bMasterOnly = $save;
       if($row = $this->ccdSQL->fetch_assoc($res)) 
       {
//					file_put_contents('./post.log',print_r($row,true)."\n\n",FILE_APPEND);
           
           return $row['d'];
        }
       return "";
   }
   function write($sessID,$sessData) {

       $save = $this->ccdSQL->bMasterOnly;
       $this->ccdSQL->bMasterOnly = true;

       // new session-expire-time
       $newExp = time() + $this->lifeTime;
       // is a session with this id in the database?
       $res = $this->ccdSQL->query("SELECT * FROM ws_sessions
                           WHERE session_id = '$sessID' AND session_host = '".$_SERVER['HTTP_HOST']."'",true) or print "ERROR";
       // if yes,

       $this->ccdSQL->bMasterOnly = $save;

       if($this->ccdSQL->num_rows($res)) {
           // ...update session-data
           $this->ccdSQL->query("UPDATE ws_sessions
                         SET session_expires = '$newExp',
                         session_data = '$sessData'
                         WHERE session_id = '$sessID' AND session_host = '".$_SERVER['HTTP_HOST']."'",true);
           // if something happened, return true
           if($this->ccdSQL->affected_rows())
               return true;
       }
       // if no session-data was found,
       else {
           // create a new row
           $this->ccdSQL->query("INSERT INTO ws_sessions (
                         session_id,
                         session_expires,
                         session_data,
                         session_host
                         )
                         VALUES(
                         '$sessID',
                         '$newExp',
                         '$sessData',
                         '".$_SERVER['HTTP_HOST']."'
                         )",true);
           // if row was created, return true
           if($this->ccdSQL->affected_rows()) {
               return true;
           } else {
           	//die("WRITE FAILED");
           }
       }
       // an unknown error occured
       return false;
   }
   function destroy($sessID) {
       // delete session-data
       $this->ccdSQL->query("DELETE FROM ws_sessions WHERE session_id = '$sessID' AND session_host = '".$_SERVER['HTTP_HOST']."'",true);
       // if session was deleted, return true,
       if($this->ccdSQL->affected_rows())
           return true;
       // ...else return false
       return false;
   }
   function gc($sessMaxLifeTime) {
       // delete old sessions
       $this->ccdSQL->query("DELETE FROM ws_sessions WHERE session_expires < ".time(),true);
       // return affected rows
       return $this->ccdSQL->affected_rows();
   }
} 
